<template>
  <div class="icons-container">
    <el-tabs type="border-card">
      <el-tab-pane label="常用">
        <div class="grid">
          <div v-for="item of commonProgramList" :key="item" @click="handleProgramJump(item,$event)">
            <el-tooltip placement="top">
              <div slot="content">
                {{ item.title }}
              </div>
              <div class="icon-item" :to="resolvePath(item.path)">
                <span>{{ item.title }}</span>
              </div>
            </el-tooltip>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getApplications,getApplicationByCompany } from '@/api/program/application'
import { getFrequentlys } from '@/api/program/frequently'
import path from 'path'
import { isExternal } from '@/utils/validate'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'Icons',
  components: { Pagination },
  data() {
    return {
      basePath: '',
      list: null,
      usableList:[],
      usableTotal:0,
      total: 0,
      listLoading: true,
      listQuery: {
        start: 1,
        perpage: 10,
        keyword: undefined,
        begin_time: undefined,
        end_time: undefined,
        sort: '+id'
      },
      commonProgramList: [
        {
          title: '题库',
          path: '#/examination/workbrench',
          icon: ''
        },
        {
          title: '机构',
          path: '#/mechanism/workbrench',
          icon: ''
        },
        {
          title: '升学',
          path: '#/education/workbrench',
          icon: ''
        },
        {
          title: '产品',
          path: '#/supply/workbrench',
          icon: ''
        },
        {
          title: '客户',
          path: '#/customer/workbrench',
          icon: ''
        },
        {
          title: '科研',
          path: '#/development/workbrench',
          icon: ''
        },
        {
          title: '知识库',
          path: '#/knowledge/workbrench',
          icon: ''
        }
      ]
    }
  },
  created() {
    console.log('初始化目录')
    this.commonProgramList = window.localStorage.getItem("common_program_list")
    console.log(this.commonProgramList)
    let formData = {
      "refrefer":window.location.href.split('#')[1]
    }
    getApplicationByCompany(formData).then((res)=>{
      if(res.status == 200){
        this.commonProgramList = res.data.list;
      }
    });
  },
  methods: {
    getUsableList() {
      this.listLoading = true
      getFrequentlys(this.listQuery).then(response => {
        this.usableList = response.data.list
        this.usableTotal = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    getList() {
      this.listLoading = true
      getApplications(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleProgramJump: function(row) {
      let hostUrl = window.location.href.split('#')[0]
      let _baseUrl = hostUrl+"#"+row.path;
      console.log(_baseUrl)
      window.location.href = _baseUrl
    },
    resolvePath(routePath) {
      if (isExternal(routePath)) {
        return routePath
      }
      if (isExternal(this.basePath)) {
        return this.basePath
      }
      return path.resolve(this.basePath, routePath)
    }
  }
}
</script>

<style lang="scss" scoped>
.icons-container {
  margin: 10px 20px 0;
  overflow: hidden;

  .grid {
    position: relative;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }

  .icon-item {
    margin: 20px;
    height: 85px;
    text-align: center;
    width: 100px;
    float: left;
    font-size: 30px;
    color: #24292e;
    cursor: pointer;
  }

  span {
    display: block;
    font-size: 16px;
    margin-top: 10px;
  }

  .disabled {
    pointer-events: none;
  }
}
</style>
